package com.hejia.alauda.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.Financing;
import com.hejia.alauda.entity.Interest;
import com.hejia.alauda.entity.User;
import com.hejia.alauda.entity.example.FinancingExample;
import com.hejia.alauda.persistence.BaseMapper;
import com.hejia.alauda.persistence.mybatis.FinancingMapper;
import com.hejia.alauda.persistence.mybatis.InterestMapper;
import com.hejia.alauda.utils.DateTimeUtil;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.vos.InterestVo;

@Service
public class InterestServiceImpl extends BaseServiceImpl<Interest> implements InterestService {
	
	@Resource
	private InterestMapper interestMapper;
	
	@Override
	protected BaseMapper<Interest> getBaseMapper() {
		return interestMapper;
	}
	
	@Resource
	private FinancingMapper financingMapper;
	
	@Override
	public Pager<InterestVo> findInterestlistByDailyAndUser(User user, Pager<InterestVo> pager, int days) {
		
		// 根据分页查询收益详情
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("user_id", user.getId());
		if(days > 0){
			params.put("days", days);
		}
		List<InterestVo> InterestVo = this.interestMapper.selectInterestlistByDaily(params, pager);
	
		for(InterestVo vo : InterestVo){
			//查询当天利息信息
			List<Interest> interests= this.interestMapper.selectInterestByValueDate(vo.getValueDate(), user.getId());
			if(interests == null || interests.isEmpty()) continue;
			vo.setInterests(interests);
			
			//查询是否一次性还款
			for(Interest interest : vo.getInterests()){
				Financing financing = interest.getFinancing();
				interest.setName(financing.getName());
				
				// 查询当前用的购买的金融包,判断金融包是否存在提前回款
				Date repaymentTime = financing.getRepaymentTime();  //还款时间
				Date valueDate = interest.getValueDate();           //利息记录时间
				Integer occupiedDays = financing.getOccupiedDays(); //实际占用天数
				Integer minterm = financing.getMinTerm();			//最低借款天数
				
				/**
				 * 提前还款判断条件: 
				 * 1. 还款时间和世纪占用天数不为空  
				 * 2. 实际占用天数小于最低借款天数
				 * 3. 当前利息记录与还款时间为同一天
				 */
				if(repaymentTime != null && occupiedDays != null){
					if((occupiedDays < minterm) && DateTimeUtil.isSameDate(valueDate, repaymentTime)){
						interest.setAdvanceRepay(true);
					}
				}
				
			}
		}
		pager.setList(InterestVo);
		pager.setTotalCount(interestMapper.selectInterestlistByDailyWithCount(params));
		return pager;
	}

	@Override
	public Map<String, Object> findUserMaxInterestByDay(User user) {
		Map<String,Object> map = this.interestMapper.selectUserMaxInterestByDay(user.getId());
		
		if(map != null && !map.isEmpty()){
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("user_id", user.getId());
			params.put("valueDate", map.get("valueDate"));
			
//			List<Map<String, Object>> interests = this.interestMapper.selectInterestDay(params);
//			map.put("interests", interests);
		}
		return map;
	}
	
}
